package com.qk.controller;

import com.qk.PageResult;
import com.qk.anno.Log;
import com.qk.common.Result;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("/users")
    public Result getUsers(UserDto userDto) {
        PageResult<User> userPage = userService.getUsers(userDto);
        return Result.success(userPage);
    }
    @Log
    @PostMapping("/users")
    public Result addUser(@RequestBody User user) {
        userService.addUser(user);
        return Result.success();
    }
    @Log
    //批量删除
    @DeleteMapping("/users/{ids}")
    public Result deleteUsers(@PathVariable List<Integer> ids){
        log.info("批量删除用户：{}",ids);
        userService.deleteUsers(ids);
        return Result.success();
    }
    //根据id查询用户
    @GetMapping("/users/{id}")
    public Result getUserById(@PathVariable Integer id){
        log.info("根据id查询用户：{}",id);
        User user = userService.getUserById(id);
        return Result.success(user);
    }
    //修改用户
    @Log
    @PutMapping("/users")
    public Result updateUser(@RequestBody User user){
        userService.updateUser(user);
        return Result.success();
    }
    //查询所有用户
    @GetMapping("/users/list")
    public Result listUsers(){
        List<User> users = userService.listUsers();
        return Result.success(users);
    }
    //根据角色查询用户
    @GetMapping("/users/role/{roleLabel}")
    public Result listUsersByRoleLabel(@PathVariable String roleLabel){
        List<User> users = userService.listUsersByRoleLabel(roleLabel);
        return Result.success(users);
    }
    //根据部门查询用户
    @GetMapping("/users/dept/{deptId}")
    public Result listUsersByDeptId(@PathVariable Integer deptId){
        List<User> users = userService.listUsersByDeptId(deptId);
        return Result.success(users);
    }


}
